<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input     v-model="dataForm.key" placeholder="手机号查找" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()" type="primary">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="id"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="">-->
      <!--      </el-table-column>-->
      <el-table-column
        width="85px"
        prop="realName"
        header-align="center"
        align="center"
        label="快递上填的名字">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="userId"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="用户id">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        label="快递上的手机号">
      </el-table-column>
      <el-table-column
        width="200px"
        prop="deliveryAddress"
        header-align="center"
        align="center"
        label="快递地址">
      </el-table-column>
      <el-table-column
        prop="orderId"
        header-align="center"
        align="center"
        label="订单id">
      </el-table-column>


      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="更新时间">
      </el-table-column>

      <el-table-column
        prop="productName"
        header-align="center"
        align="center"
        label="商品名">

      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="point"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="扣除的积分">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="expressNum"
        header-align="center"
        align="center"
        label="快递单号">
        <template slot-scope="scope">
          <div v-if="scope.row.expressNum !== null">
            {{ scope.row.expressNum  }}
          </div>
          <el-button size="mini" v-else style="background-color: #f8839d;color: white" @click="clickInputExpress(scope.row.orderId)">
            填写订单号
          </el-button>
        </template>
      </el-table-column>
      <el-dialog
        title="输入订单号"
        :visible.sync="inputExpress"
        width="30%"
        append-to-body>
        <el-input v-model="express"></el-input>
        <span slot="footer" class="dialog-footer">
    <el-button @click="inputExpress = false">取 消</el-button>
    <el-button type="primary" @click="clickInputExpressConfirm()">确 定</el-button>
  </span>
      </el-dialog>
      <el-table-column
       width="125px"
        prop="area"
        header-align="center"
        align="center"
        label="订单操作">
        <template slot-scope="scope">
          <div v-if="scope.row.expressNum !== null">
            <el-button size="mini" style="background-color: #41baff;color: white"  @click="clickInputExpress(scope.row.orderId)">修改快递单号</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="area"
        header-align="center"
        align="center"
        label="送货地区">
      </el-table-column>

      <!--      <el-table-column-->
      <!--        fixed="right"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        width="150"-->
      <!--        label="操作">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
      <!--          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './mallorder-add-or-update'

export default {
  data () {
    return {
      dataForm: {
        key: '',
      },
      inputExpress: false,
      express:'',
      orderId:'',
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      expressNum: '',

    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/mall/mallorder/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    clickInputExpress (orderId) {

      this.inputExpress = true
        this.orderId = orderId
    },
    clickInputExpressConfirm(){

      this.$confirm("请确认是否提交快递单号！","提示",{
        iconClass: "el-icon-warning",//自定义图标样式
        confirmButtonText: "确认",//确认按钮文字更换
        cancelButtonText: "取消",//取消按钮文字更换
        showClose: true,//是否显示右上角关闭按钮
        type: "warning",//提示类型  success/info/warning/error
      }).then(()=>{
        this.$http({
          url: this.$http.adornUrl("/mall/mallorder/express/num"),
          method: 'get',
          params: this.$http.adornParams({
            'orderId': this.orderId,
            'expressNum': this.express,
            // 'teacherName':this.dataForm.teacherName
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.inputExpress = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
        }
      )
        .catch(function (err) {
        //捕获异常
      })

    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/modules/mallorder/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
